<template>
	<div class="add-jq-dialog">
		<!--版本升级-->
		
		<appdialog 
			ref="dialog"
			:visible="false" 
			title="升级"
			width="30%"
			@confirm="confirmAdd"
			@cancle="cancleAdd"
			>
			
			<el-form 
				ref="addForm" 
				:rules="rules" 
				:model="addForm"
				label-width="100px"
				>
				
				<el-alert
			    :title="'当前版本：' + appVersion"
			    type="info"
			    :closable="false"
			    show-icon>
			  </el-alert>

			

				<el-form-item  label="目标版本：" prop="appVersion" v-if="isShow">
					<el-input style="width: 160px;" v-model="addForm.appVersion"></el-input>
				</el-form-item>
			   
				<el-form-item  label="目标版本：" prop="appVersion" v-else>
					<el-select v-model="addForm.appVersion" style="width: 160px;" @change="handleChooseVers">
						<el-option v-for="item in versionList" 
						:key="item.index" 
						:value="item" 
						:value-key="'label'"
						:label="item.version"></el-option>
					</el-select>
				</el-form-item>
				
			</el-form>
			
		</appdialog>
		
	</div>
</template>

<script>

import appdialog from "@/view/components/dialog/dialog";

import {upgrade} from "@/service/swagger/AppBuilderController"

export default {
	components: {
		appdialog,
	},
	props:[
		
	],
	data() {
    return {
			copyData:[],
			versionList:[],
			isShow:true,
    	appVersion : "1.0" ,
      addForm : {
      	sourceTemplateId:"",
				appVersion : "",
				filePath:'',
      },
      rules : {
      	appVersion: [
          { required: true, message: '请输入目标版本', trigger: 'blur' },
        ],
      }
    }
  },
  mounted() {
  	
  },
	methods:{
		init(options){
			this.addForm.sourceTemplateId = options.sourceTemplateId || "";
			this.appVersion = options.appVersion || "";
			return this ;
		},
		
   	open(data){
			 this.copyData = data
			if(data.length){
				this.versionList = data
				this.isShow = false
			}else{
				this.versionList = []
				this.isShow = true
			}
   		this.$refs.dialog.open();
   	},
   	handleChooseVers(){
				console.log(this.addForm.appVersion)
				this.addForm.filePath = 'file://' + this.addForm.appVersion.filePath
				this.addForm.appVersion = this.addForm.appVersion.version
				console.log(this.addForm)
		},
   	confirmAdd(){
   		// 验证表单
   		this.$refs.addForm.validate((valid) => {
        if (valid) {
        	// 验证通过
					this.submitAdd();
					this.cancleAdd()
        } else {
        	setTimeout(()=>{
        		this.open(this.copyData);
        	},50)
          return false;
        }
      });
   	},
   	submitAdd(){
			 console.log(this.addForm)
   		upgrade({...this.addForm}).then(ret=>{
   			this.$message({
          message: '请求成功',
          type: 'success',
          duration : 2000,
        });
           
        this.$refs.dialog.close();
        this.$emit("addSucess")
   		})
   	},
   	cancleAdd(){
			this.copyData = []
   		this.addForm = {
				sourceTemplateId:"",
				appVersion : "",
				filePath:'',
			}
			this.$refs.addForm.clearValidate(['appVersion'])
   	},
   	
   	
	}
}
</script>

<style scoped lang="scss">
@import "../../../styles/variables.scss";


</style>